/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * InsercionPreguntasRespuestas.java
 *
 * Created on 15-ene-2011, 21:00:21
 */

package Vista.Tarea;

import Modelo.Entidades.Pregunta;
import Modelo.Entidades.PreguntaImpl;
import Modelo.Entidades.Respuesta;
import Modelo.Entidades.RespuestaImpl;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;

/*
 *  Proyecto eJAM [G12]
 *  Miembros:
 *      - Javier Ruiz Hidalgo
 *      - Adrian Ramos Cápitas
 *      - Miguel Alejandro Esteban Ordóñez
 */

public class InsercionPreguntasRespuestas extends javax.swing.JFrame {

    private CrearTareaView view;
    private RespuestasTableAndComboModel respuestasTableAndComboModel;
    private List<Respuesta> respuestasProvisional = null;
    private List<Pregunta> preguntasProvisional = null;

    /** Creates new form InsercionPreguntasRespuestas */
    public InsercionPreguntasRespuestas(CrearTareaView view) {
        this.view = view;
        respuestasProvisional = new ArrayList();
        preguntasProvisional = new ArrayList();
        respuestasTableAndComboModel = RespuestasTableAndComboModel.create();
        
        initComponents();

        setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jFinalizar = new javax.swing.JButton();
        jBorrar = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JSeparator();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jAreaEnunciadoPregunta = new javax.swing.JTextArea();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel2 = new javax.swing.JLabel();
        jRespuesta = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jComboCorrecta = new javax.swing.JComboBox();
        jAñadirPregunta = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTablaRespuestas = new javax.swing.JTable();
        jAñadirRespuesta = new javax.swing.JButton();
        jSeparator3 = new javax.swing.JSeparator();
        jSeparator4 = new javax.swing.JSeparator();
        jPanelIntroducidas = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jAreaPreguntasIntro = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Inserción de Preguntas y Respuestas");

        jFinalizar.setText("Finalizar");
        jFinalizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jFinalizarActionPerformed(evt);
            }
        });

        jBorrar.setText("Borrar");
        jBorrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBorrarActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Nueva Pregunta", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N

        jLabel1.setText("Enunciado Pregunta: ");

        jAreaEnunciadoPregunta.setColumns(20);
        jAreaEnunciadoPregunta.setLineWrap(true);
        jAreaEnunciadoPregunta.setRows(5);
        jAreaEnunciadoPregunta.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
        jAreaEnunciadoPregunta.setPreferredSize(new java.awt.Dimension(164, 50));
        jScrollPane2.setViewportView(jAreaEnunciadoPregunta);

        jLabel2.setText("Posible Respuesta:");

        jRespuesta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRespuestaActionPerformed(evt);
            }
        });

        jLabel3.setText("Respuesta Correcta: ");

        jComboCorrecta.setModel(respuestasTableAndComboModel);

        jAñadirPregunta.setText("Añadir Pregunta");
        jAñadirPregunta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jAñadirPreguntaActionPerformed(evt);
            }
        });

        jTablaRespuestas.setModel(respuestasTableAndComboModel);
        jTablaRespuestas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTablaRespuestasMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(jTablaRespuestas);

        jAñadirRespuesta.setText("Añadir Respuesta");
        jAñadirRespuesta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jAñadirRespuestaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jSeparator4, javax.swing.GroupLayout.DEFAULT_SIZE, 748, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(18, 18, 18)
                                .addComponent(jComboCorrecta, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addComponent(jAñadirRespuesta, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(30, 30, 30)
                                .addComponent(jRespuesta))
                            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(18, 18, 18)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 302, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jSeparator3, javax.swing.GroupLayout.Alignment.LEADING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 308, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jAñadirPregunta, javax.swing.GroupLayout.DEFAULT_SIZE, 748, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addComponent(jLabel1))
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jRespuesta, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addComponent(jAñadirRespuesta)
                        .addGap(18, 18, 18)
                        .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jComboCorrecta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(18, 18, 18)
                .addComponent(jSeparator4, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jAñadirPregunta, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanelIntroducidas.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Preguntas Introducidas", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N

        jAreaPreguntasIntro.setColumns(20);
        jAreaPreguntasIntro.setEditable(false);
        jAreaPreguntasIntro.setRows(5);
        jScrollPane1.setViewportView(jAreaPreguntasIntro);

        javax.swing.GroupLayout jPanelIntroducidasLayout = new javax.swing.GroupLayout(jPanelIntroducidas);
        jPanelIntroducidas.setLayout(jPanelIntroducidasLayout);
        jPanelIntroducidasLayout.setHorizontalGroup(
            jPanelIntroducidasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelIntroducidasLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 748, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanelIntroducidasLayout.setVerticalGroup(
            jPanelIntroducidasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelIntroducidasLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 177, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanelIntroducidas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jSeparator2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 780, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jFinalizar, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jBorrar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanelIntroducidas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBorrar)
                    .addComponent(jFinalizar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jFinalizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jFinalizarActionPerformed
        if(preguntasProvisional.size() > 0){
            view.preguntas.clear();
            view.preguntas.addAll(preguntasProvisional);
            view.cuentaPreguntas();
            this.removeAll();
            this.setVisible(false);
        }
        else{
            JOptionPane.showMessageDialog(null, "Error: No Hay Preguntas Insertadas");
        }
}//GEN-LAST:event_jFinalizarActionPerformed

    private void jBorrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBorrarActionPerformed
        respuestasProvisional.clear();
        preguntasProvisional.clear();
        this.jAreaEnunciadoPregunta.setText("");
        this.jAreaPreguntasIntro.setText("");
        this.jRespuesta.setText("");
        this.jComboCorrecta.removeAllItems();
        this.respuestasTableAndComboModel.setRespuestas(respuestasProvisional);
}//GEN-LAST:event_jBorrarActionPerformed

    private void jRespuestaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRespuestaActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_jRespuestaActionPerformed

    private void jAñadirPreguntaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jAñadirPreguntaActionPerformed
        boolean confirmacion = true;
        Pregunta pregunta = null;

        for(Pregunta p: preguntasProvisional){
            if(p.getDescripcion().equalsIgnoreCase(this.jAreaEnunciadoPregunta.getText())){
                confirmacion = false;
            }
        }

        if(confirmacion == true){
            if(this.jAreaEnunciadoPregunta.getText().equals("")){
                JOptionPane.showMessageDialog(null, "Error: Enunciado de Pregunta Vacio");
            }
            else{
                if(respuestasProvisional.size() > 1){
                    if(this.jComboCorrecta.getSelectedIndex() > -1){
                        pregunta = new PreguntaImpl(preguntasProvisional.size());
                        pregunta.setDescripcion(this.jAreaEnunciadoPregunta.getText());
                        pregunta.setrCorrecta(this.jComboCorrecta.getSelectedIndex());
                        pregunta.getRespuestas().addAll(respuestasProvisional);
                        respuestasProvisional.clear();
                        preguntasProvisional.add(pregunta);

                        this.jAreaEnunciadoPregunta.setText("");
                        this.jRespuesta.setText("");
                        this.jComboCorrecta.setSelectedIndex(-1);

                        refresh();
                        muestraPreguntas();
                    }
                    else{
                        JOptionPane.showMessageDialog(null, "Error: No Seleccionada Ninguna Respuesta Correcta");
                    }
                }
                else {
                    JOptionPane.showMessageDialog(null, "Error: Número de Respuestas Insuficiente");
                }
            }
        }
        else{
            JOptionPane.showMessageDialog(null, "Error: Enunciado de Pregunta Ya Registrado");
        }
}//GEN-LAST:event_jAñadirPreguntaActionPerformed

    private void jTablaRespuestasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTablaRespuestasMouseClicked
        RespuestasTableAndComboModel ctm = (RespuestasTableAndComboModel)this.jTablaRespuestas.getModel();
        Respuesta respuesta = ctm.getRow(this.jTablaRespuestas.getSelectedRow());
        jComboCorrecta.setSelectedItem(respuesta);
}//GEN-LAST:event_jTablaRespuestasMouseClicked

    private void jAñadirRespuestaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jAñadirRespuestaActionPerformed
        if(this.jRespuesta.getText().equals("")){
            JOptionPane.showMessageDialog(null, "Error: Respuesta Vacia");
        }
        else{
            this.respuestasProvisional.add(new RespuestaImpl(respuestasProvisional.size(), this.jRespuesta.getText()));
            refresh();
            this.jRespuesta.setText("");
        }
}//GEN-LAST:event_jAñadirRespuestaActionPerformed

    
    /**
    * @param args the command line arguments
    */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea jAreaEnunciadoPregunta;
    private javax.swing.JTextArea jAreaPreguntasIntro;
    private javax.swing.JButton jAñadirPregunta;
    private javax.swing.JButton jAñadirRespuesta;
    private javax.swing.JButton jBorrar;
    private javax.swing.JComboBox jComboCorrecta;
    private javax.swing.JButton jFinalizar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanelIntroducidas;
    private javax.swing.JTextField jRespuesta;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JTable jTablaRespuestas;
    // End of variables declaration//GEN-END:variables

    public void refresh(){
        respuestasTableAndComboModel.setRespuestas(respuestasProvisional);
    }

    private void muestraPreguntas() {
        String s = "";

        for(int i = 0; i < preguntasProvisional.size(); i++){
            s += preguntasProvisional.get(i).toString();
            s += "\n\n";
        }

        this.jAreaPreguntasIntro.setText(s);
    }

}
